package com.eachonline.smart.web.user.order;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.model.SmartOrderRecord;
import com.eachonline.smart.model.SmartOrderRecordExample;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.model.SmartUserExample;
import com.eachonline.smart.model.base.BaseSmartOrderRecordExample.Criteria;
import com.eachonline.smart.service.CourseService;
import com.eachonline.smart.service.OrderRecordService;
import com.eachonline.smart.service.SmartUserService;
import com.eachonline.smart.service.UserCourseService;
import com.eachonline.smart.util.OrderStatus;
import com.eachonline.smart.web.common.CommonUtil;
import com.eachonline.util.StringUtil;

@Controller
@RequestMapping("/order")
public class SmartOrderRcordController {
	
	@Autowired
	public CourseService coursesService;
	
	@Autowired
	public UserCourseService userCourseService;
	
	@Autowired
	public SmartUserService userService;
	
	
	@Autowired
	public OrderRecordService orderRecordService;
	
	
	 /**
     * 我的购买订单
     * @param session
     * @return  
	 * @throws UnsupportedEncodingException 
     * @Description:O
     */
    @RequestMapping(value="/myOrderRecord", method = { RequestMethod.POST })
    @ResponseBody
	public JsonResult  myOrderRecord(HttpServletRequest request,String orderStatus) 
	{
    	int start=0;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		
		int begin =pageSize*(start-1);
		SmartUser user=CommonUtil.getCurrentUser(request);
		
		SmartOrderRecordExample ore=new SmartOrderRecordExample();
		Criteria cri= ore.createCriteria();
		cri.andUserIdEqualTo(user.getSid());
		if(StringUtil.isNotEmpty(orderStatus))
		{
			cri.andOrderStatusEqualTo(orderStatus);
		}
		
		Pagination<SmartOrderRecord>  pageInfo=orderRecordService.queryByPage(ore, begin, pageSize,"trade_time desc");
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("pageInfo", pageInfo);
		return new JsonResult(true, "成功", resultMap);
		
		
	}
    
    
    
    
    /**
     * 订单管理购买订单
     * @param session
     * @return  
	 * @throws UnsupportedEncodingException 
     * @Description:O
     */
    @RequestMapping(value="/manager/OrderRecordList", method = { RequestMethod.POST })
    @ResponseBody
	public JsonResult  managerOrderRecord(HttpServletRequest request,String phone,String orderNo) 
	{
    	int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		
		int begin =pageSize*(start-1);
		SmartOrderRecordExample ore=new SmartOrderRecordExample();
		Criteria cri=ore.createCriteria();
		
		if(StringUtil.isNotEmpty(phone))
		{
			 SmartUserExample exa=new SmartUserExample();
			 exa.createCriteria().andLoginNameEqualTo(phone);
			 List<SmartUser>  list=userService.selectByExample(exa);
			if(list.isEmpty())
			{
				return new JsonResult(false, "用户不存在");
			}
			cri.andUserIdEqualTo(list.get(0).getSid());
		}
		if(StringUtil.isNotEmpty(orderNo))
		{
			
			cri.andOrderNoEqualTo(orderNo);
		}
	    
		
		Pagination<SmartOrderRecord>  pageInfo=orderRecordService.queryByPage(ore, begin, pageSize,"trade_time desc");
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("pageInfo", pageInfo);
		return new JsonResult(true, "成功", resultMap);
		
		
	}
    

  /**
   * 关闭订单
   * @param session
   * @return  
	 * @throws UnsupportedEncodingException 
   * @Description:O
   */
  @RequestMapping(value="/manager/cancelOrder", method = { RequestMethod.POST })
  @ResponseBody
	public JsonResult  managerCancelOrder(HttpServletRequest request,String orderId) 
	{
  	
		SmartOrderRecord orderRecord=orderRecordService.selectByPrimaryKey(orderId);
		
		if(orderRecord==null)
		{
			return new JsonResult(false, "订单不存在");
		}
		orderRecord.setOrderStatus(OrderStatus.FAIL);
		orderRecordService.updateByPrimaryKeySelective(orderRecord);
		return new JsonResult(true, "修改成功");
	}
  

	/**
    * 取消订单
    * @param session
    * @return  
	* @throws UnsupportedEncodingException 
    * @Description:O
    */
   @RequestMapping(value="/cancelOrder", method = { RequestMethod.POST })
   @ResponseBody
	public JsonResult  cancelOrder(HttpServletRequest request,String orderId) 
	{
   	
		SmartOrderRecord orderRecord=orderRecordService.selectByPrimaryKey(orderId);
		
		if(orderRecord==null)
		{
			return new JsonResult(false, "订单不存在");
		}
		orderRecord.setOrderStatus(OrderStatus.cancel);
		orderRecordService.updateByPrimaryKeySelective(orderRecord);
		return new JsonResult(true, "修改成功");
	}
   
		 /**
		  * 删除订单
		  * @param session
		  * @return  
		  * @throws UnsupportedEncodingException 
		  * @Description:O
		  */
		 @RequestMapping(value="/deleteOrder", method = { RequestMethod.POST })
		 @ResponseBody
		public JsonResult  deleteOrder(HttpServletRequest request,String orderId) 
		{
	 	
		    if(StringUtil.isEmpty(orderId))
		    {
		    	return new JsonResult(true, "订单Id不存在");
		    }
			orderRecordService.deleteByPrimaryKey(orderId);
			return new JsonResult(true, "删除成功");
		}

}
